<?php

require_once './include/common.inc.php';
checkLogin();
$page_title = 'jans2en furniture - Inventory - Inventory Listing';
$menu_id = 6;
$page_guide_name2 = '<a href="inventory.php">Inventory</a>';


getMyParam('inventoryid');
getMyParam('special');

// -------------- guideͼƬ start ----------------------------------------
$system_ary = getSystemValue("");
$guide_pic = $system_ary['picture_inventory'];
$page_special_pic = $system_ary['picture_specials'];

$page_guide_pic = $guide_pic;

setStatus($special);


if (trim($page_guide_pic) == "" || !file_exists("./attachments/system/" . $page_guide_pic)) {
    $guide_pic = './attachments/system/no_guide.gif';
} else {
    $guide_pic = './attachments/system/' . $page_guide_pic;
}

// -------------- guideͼƬ end ------------------------------------------



if (!is_numeric($inventoryid) || $inventoryid <=0) {
    header("Location:inventory.php");
}

if($inventoryid == '3'){
	browseTracking(15,0,'Inventory > HongKong Warehouse');
}else if($inventoryid == '1'){
	browseTracking(15,0,'Inventory > Indonesia Warehouse');
}


if ($jansen_type==2) {
    $sql_where = ' WHERE 1 = 1 ';
} elseif ($jansen_type == 1) {
    $sql_where = ' WHERE `type` = 1 ';
    //$inventoryid = 1;
}


//$sql = "SELECT `id`, `name`, `pic`, `type` FROM `inventory` " . $sql_where . " ORDER BY `seq` ASC";
$sql = "SELECT n.`id`, n.`name`, n.`pic`, n.`type`, n.`update` FROM `inventory` as n, `product_inventory` as i " . $sql_where . $sql_2 . $sql_3 . " GROUP BY n.`id` ORDER BY n.`seq` ASC";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    $inventory_show_ary[] = $rs;
    if ($inventoryid == $rs['id']) { 
        $this_type = $rs['type'];
        $this_name = $rs['name'];
        $update_time = $rs['update'] == 0 ? '01/Jan/2006' : date('d/M/Y', $rs['update']);
    }
}

$inventory_type = $this_type;

if ($this_type == 2) {
    $delete_flag_str = '';

    $sql = "SELECT `id` FROM `product` WHERE `delete_flag` = 1";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $delete_flag_ary[] = $rs['id'];   //ΪɾĲƷ
    }

    if (!empty($delete_flag_ary)) {
        $delete_flag_str = " AND c.`product_id` NOT IN (" . implode(",", $delete_flag_ary) . ") ";
    }

    $have_qty_product_ary = array();
    $sql = "SELECT c.`category_id`, c.`product_id`, i.`qty` FROM `product_inventory` i, `product_category` c WHERE i.`product_id`=c.`product_id` AND c.`seq`>0 AND i.`qty`>0 " . $sql_2 . " AND i.`inventory_id` = '$inventoryid' " . $delete_flag_str . "ORDER BY c.`seq` ASC";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $category_id_ary[] = $rs['category_id'];   //вƷķ
        $have_qty_product_ary[] = $rs['product_id'];
        $product_qty_ary[$rs['product_id']] = $rs['qty'];
    }

    $page_url = 'inventory_category.php?inventoryid=' . $inventoryid . '&';

    
    $have_qty_product_str = implode(",", $have_qty_product_ary);
	
    $sql = "SELECT p.`code` FROM `product` p, `product_category` c, `category` ca WHERE ca.id=c.`category_id` AND p.`id`=c.`product_id` AND p.`id` IN (" . $have_qty_product_str . ") GROUP BY p.`id` ORDER BY ca.fid asc, ca.seq asc, p.`code` DESC, p.`id` DESC";
    $query = $db->query($sql);
    $sql_all_num = $db->num_rows($query);
    
    //$sql_all_num = count($have_qty_product_ary);
    $ppp = 12;
    getMyParam('page');
    $page = empty($page) || !ispage($page) ? 1 : $page;
    $start_limit = ($page - 1) * $ppp;
    if($start_limit > $sql_all_num) {
        $start_limit = 0;
        $page = 1;
    }

    $page_show = getPhotoPageStr($sql_all_num, $page, $ppp, $page_url);

    $product_ary = array();
    $sql = "SELECT p.`id`, p.`code`, p.`name`, p.`pic`, p.`small_pic`, p.`price_1`, p.`price_2`, p.`price_3`, p.`new_flag` FROM `product` p, `product_category` c, `category` ca WHERE ca.id=c.`category_id` AND p.`id`=c.`product_id` AND p.`id` IN (" . $have_qty_product_str . ") GROUP BY p.`id` ORDER BY ca.fid asc, ca.seq asc, p.`code` DESC, p.`id` DESC LIMIT $start_limit, $ppp";
    //echo $sql;
    //echo $sql;exit;
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $product_ary[] = $rs;
        $js_id_ary[] = "'" . $rs['id'] . "'";
        $sp_id_ary[] = $rs['id'];
    }

    if (!empty($sp_id_ary)) {
        $sp_id_str = implode(",", $sp_id_ary);
        $sql = "SELECT `product_id`, `qty`, `special_price`, `special_price2` FROM `product_inventory` WHERE `product_id` IN (" . $sp_id_str . ") AND `inventory_id`='$inventoryid'";
        $query = $db->query($sql);
        while($rs = $db->fetch_array($query)) {
            $sp_ary[$rs['product_id']] = $rs['special_price'];
            $sp_ary2[$rs['product_id']] = $rs['special_price2'];
        }
    }

    $category_pic_2 = 'system/' . $page_guide_pic;
    $js_id_ary_str = !empty($js_id_ary) ? "new Array(" . implode(',', $js_id_ary) . ")" : "new Array()";

    require_once PrintEot('header');
    require_once PrintEot('inventory_list');
    require_once PrintEot('footer');
    exit;
} else {
    //delete_flag=1ĲƷʾ
    $delete_flag_str = '';

    $sql = "SELECT `id` FROM `product` WHERE `delete_flag` = 1";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $delete_flag_ary[] = $rs['id'];   //ΪɾĲƷ
    }

    if (!empty($delete_flag_ary)) {
        $delete_flag_str = " AND c.`product_id` NOT IN (" . implode(",", $delete_flag_ary) . ") ";
    }


    // ===== ֻʾвƷûвƷͲʾ start ======
    // inventory  categoryһ
    $sql = "SELECT c.`category_id`, i.`inventory_id` FROM `product_inventory` i, `product_category` c WHERE i.`product_id`=c.`product_id` AND c.`seq`>0 AND i.`qty`>0 " . $sql_2 . " AND i.`inventory_id` = $inventoryid" . $delete_flag_str;
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        //$category_id_ary[$rs['inventory_id']][] = $rs['category_id'];
        $category_id_ary[] = $rs['category_id'];
    }

    $category_id_ary = @array_unique($category_id_ary);
    $category_id_str = @implode(",", $category_id_ary);

    if (!empty($category_id_ary)) {
        $sql = "SELECT `id`, `name`, `pic`, `seq`, `fid`, `description` FROM `category` WHERE `fid`>0 AND `id` IN (" . $category_id_str . ")";
        $query = $db->query($sql);
        while($rs = $db->fetch_array($query)) {
            $category_id_ary[] = $rs['fid'];
        }
        $category_id_ary = array_unique($category_id_ary);
        $category_id_str = implode(",", $category_id_ary);
    }

    // === end ========================

    if (!empty($category_id_ary)) {
        $sql = "SELECT `id`, `name`, `pic`, `seq`, `fid`, `description` FROM `category` WHERE `id` IN (" . $category_id_str . ") ORDER BY `seq` ASC";
        $query = $db->query($sql);
        while($rs = $db->fetch_array($query)) {
            if ($rs['fid']==0) {
                $out_ary[$rs['id']] = $rs;
                $sub_category_ary2[$rs['id']][] = $rs['id'];
            } else {
                $sub_category_ary[$rs['fid']][] = $rs['id'];
                $sub_category_ary2[$rs['fid']][] = $rs['id'];
            }
        }

        if (!empty($sub_category_ary2)) {
            foreach ($sub_category_ary2 as $k=>$v) {
                $temp_str = implode(",", $v);
                $sql = "SELECT p.`small_pic`, p.`id` FROM `product` p, `product_category` c where p.id=c.product_id AND c.category_id IN (" . $temp_str . ") AND p.`small_pic` != '' ORDER BY RAND( ) limit 1 ";
                $tmp_ary = $db->get_one($sql);
                
                $while_start = 1;
        		$while_end = 20;
        
        		while ($while_start<=$while_end && (trim($tmp_ary['small_pic']) == "" || !file_exists("./attachments/" . $tmp_ary['small_pic']))) {
        			$tmp_ary = $db->get_one($sql);
        			$while_start++;
        		}
                
                
                $out_ary[$k]['pic'] = @$tmp_ary['small_pic'];
            }
        }

    } else {
        $out_ary = array();
    }

    require_once PrintEot('header');
    require_once PrintEot('inventory_category');
    require_once PrintEot('footer');
}




?>